class ExamController {

    static allowedMethods = [save: "POST", update: "POST"]
    def timeUtilService

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 20, 100)
        def classes = Classes.get(params.id)
        if (classes && params.course) {
                            [examInstanceList: Exam.findAllByClassesAndCourse(classes , params.course , params), 
                examInstanceTotal: Exam.countByClassesAndCourse(classes , params.course) , 
                classes : classes ,
                course : params.course] 
        }else{
            [examInstanceList: [], examInstanceTotal: 0]
        }
    }

    def create = {
        def examInstance = new Exam()
        examInstance.properties = params
        return [examInstance: examInstance]
    }

    def save = {
        params.startTime =  timeUtilService.stringToDate(params.startTime)

        def examInstance = new Exam(params)
        examInstance.school = session.school
        examInstance.grade = examInstance?.classes?.grade
        examInstance.team = examInstance?.classes?.team
        examInstance.term = Term.currentTerm()
   
        if (examInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'exam.label', default: 'Exam'), examInstance])}"
            redirect(action: "list" , params: [id: examInstance?.classes?.id , course : examInstance?.course])
        }
        else {
            render(view: "create", model: [examInstance: examInstance])
        }
    }

    def show = {
        def examInstance = Exam.get(params.id)
        if (!examInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'exam.label', default: 'Exam'), params.id])}"
            redirect(action: "list")
        }
        else {
            [examInstance: examInstance]
        }
    }

    def edit = {
        def examInstance = Exam.get(params.id)
        if (!examInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'exam.label', default: 'Exam'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [examInstance: examInstance]
        }
    }

    def update = {
        params.startTime =  timeUtilService.stringToDate(params.startTime)

        def examInstance = Exam.get(params.id)
        if (examInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (examInstance.version > version) {
                    
                    examInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'exam.label', default: 'Exam')] as Object[], "Another user has updated this Exam while you were editing")
                    render(view: "edit", model: [examInstance: examInstance])
                    return
                }
            }
            examInstance.properties = params
            if (!examInstance.hasErrors() && examInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'exam.label', default: 'Exam'), examInstance])}"
                redirect(action: "list", params: [id: examInstance?.classes?.id , course : examInstance?.course])
            }
            else {
                render(view: "edit", model: [examInstance: examInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'exam.label', default: 'Exam'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def examInstance = Exam.get(params.id)
        if (examInstance) {
            try {
                def scores = Score.findAllByExam(examInstance) 
                scores.each {   
                    it.delete(flush: true)
                } 
                examInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'exam.label', default: 'Exam'), examInstance])}"
                redirect(action: "list", params: [id: examInstance?.classes?.id , course : examInstance?.course])
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'exam.label', default: 'Exam'), examInstance])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'exam.label', default: 'Exam'), params.id])}"
            redirect(action: "list")
        }
    }
}
